package com.office.controller;

import com.office.common.annotation.Log;
import com.office.common.config.RuoYiConfig;
import com.office.common.core.controller.BaseController;
import com.office.common.core.domain.AjaxResult;
import com.office.common.core.page.TableDataInfo;
import com.office.common.enums.BusinessType;
import com.office.common.utils.file.FileUploadUtils;
import com.office.common.utils.file.MimeTypeUtils;
import com.office.common.utils.poi.ExcelUtil;
import com.office.domain.SysFeed;
import com.office.service.ISysFeedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 意见反馈Controller
 * 
 * @author yin
 * @date 2024-07-16
 */
@RestController
@RequestMapping("/system/feed")
public class SysFeedController extends BaseController
{
    @Autowired
    private ISysFeedService sysFeedService;

    /**
     * 查询意见反馈列表
     */
    @PreAuthorize("@ss.hasPermi('system:feed:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysFeed sysFeed)
    {
        startPage();
        List<SysFeed> list = sysFeedService.selectSysFeedList(sysFeed);
        return getDataTable(list);
    }

    /**
     * 导出意见反馈列表
     */
    @PreAuthorize("@ss.hasPermi('system:feed:export')")
    @Log(title = "意见反馈", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysFeed sysFeed)
    {
        List<SysFeed> list = sysFeedService.selectSysFeedList(sysFeed);
        ExcelUtil<SysFeed> util = new ExcelUtil<SysFeed>(SysFeed.class);
        util.exportExcel(response, list, "意见反馈数据");
    }

    /**
     * 获取意见反馈详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:feed:query')")
    @GetMapping(value = "/{feedId}")
    public AjaxResult getInfo(@PathVariable("feedId") Long feedId)
    {
        return success(sysFeedService.selectSysFeedByFeedId(feedId));
    }

    /**
     * 新增意见反馈
     */
    @PreAuthorize("@ss.hasPermi('system:feed:add')")
    @Log(title = "意见反馈", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysFeed sysFeed)
    {
        sysFeed.setFeedTime(new Date());
        return toAjax(sysFeedService.insertSysFeed(sysFeed));
    }

    /**
     * 修改意见反馈
     */
    @PreAuthorize("@ss.hasPermi('system:feed:edit')")
    @Log(title = "意见反馈", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysFeed sysFeed)
    {
        return toAjax(sysFeedService.updateSysFeed(sysFeed));
    }

    /**
     * 删除意见反馈
     */
    @PreAuthorize("@ss.hasPermi('system:feed:remove')")
    @Log(title = "意见反馈", businessType = BusinessType.DELETE)
	@DeleteMapping("/{feedIds}")
    public AjaxResult remove(@PathVariable Long[] feedIds)
    {
        return toAjax(sysFeedService.deleteSysFeedByFeedIds(feedIds));
    }

    /**
     * 图片上传
     */
    @Log(title = "反馈图片", businessType = BusinessType.UPDATE)
    @PostMapping("/feedbackImg")
    public AjaxResult feedbackImg(@RequestParam("avatarfile") MultipartFile file) throws Exception
    {
        if (!file.isEmpty())
        {
            String imgUrl = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            System.out.println(imgUrl);
                AjaxResult ajax = AjaxResult.success();
                ajax.put("imgUrl", imgUrl);
                // 更新缓存用户头像
//                loginUser.getUser().setAvatar(avatar);
//                tokenService.setLoginUser(loginUser);
                return ajax;
        }
        return error("上传图片异常，请联系管理员");
    }
}
